#coding:utf-8

from util.common_util import get_now_time
from util.mongodb_client import set_mongodb_connect
from week1_homework.get_58 import get_zhuanzhuan_data
from week2.week2_2.week2_2code_of_video.tongcheng58.channel_extract import get_channel
from constants.url_58 import *
from week2_2.week2_2code_of_video.tongcheng58.page_parse import get_item_link
from multiprocessing import Pool


def item_link_collect(channel_url):
    channel_urls = get_channel(channel_url, headers)

    for channel_url in channel_urls:
        get_item_link(index_url, channel_url, 100)


'''
从mongo获取商品连接，爬取商品信息
'''


def get_item_info():
    item_link_sheet = set_mongodb_connect(dataBaseInfo, itemLinkSheet)
    item_info_sheet = set_mongodb_connect(dataBaseInfo, itemInfoSheet)

    flag = True
    while flag:

        for item_link in item_link_sheet.find({'optimistic': 0}).limit(10):

            if item_link is None:
                flag = False
                break

            if item_link['item_title'].find('回收') != -1:
                item_link_sheet.update(
                    {'_id': item_link['_id']},
                    {'$set': {
                        'optimistic': int(-1),
                        'update_time': get_now_time()
                    }})
                continue

            if item_link['item_title'].find('分期') != -1:
                item_link_sheet.update(
                    {'_id': item_link['_id']},
                    {'$set': {
                        'optimistic': int(-2),
                        'update_time': get_now_time()
                    }})
                continue

            zhuanzhuan_url = item_link['link']
            data = get_zhuanzhuan_data(zhuanzhuan_url, headers)

            if data is None:
                item_link_sheet.update(
                    {'_id': item_link['_id']},
                    {'$set': {
                        'optimistic': int(-3),
                        'update_time': get_now_time()
                    }})
            else:

                data['category'] = item_link['category']
                data['item_link_id'] = item_link['_id']
                data['link'] = item_link['link']
                data['optimistic'] = 0
                data['create_time'] = get_now_time()
                data['update_time'] = get_now_time()
                print("insert item_info, data:{}".format(data))
                item_info_sheet.insert_one(data)

                # print("update item_link, data:{}".format(item_link))
                item_link_sheet.update(
                    {'_id': item_link['_id']},
                    {'$set': {
                        'optimistic': int(item_link['optimistic'] + 1),
                        'update_time': get_now_time()
                    }})


if __name__ == '__main__':
    # pool = Pool()
    # pool.map(get_item_info,"")
    get_item_info()
    # item_info_sheet = set_mongodb_connect(dataBaseInfo, itemInfoSheet)
    # item_link_sheet = set_mongodb_connect(dataBaseInfo, itemLinkSheet)
    #
    # item_infos = item_info_sheet.find({'price': {"$lt": 10}})
    # for item_info in item_infos:
    #     # item_links = item_link_sheet.find({'_id':item_info['item_link_id']})
    #     # print(item_links[0])
    #
    #     item_link_sheet.update(
    #         {'_id':item_info['item_link_id']},
    #         {'$set':{
    #             'optimistic': 0
    #         }}
    #     )
    #     print(item_info)
    #     item_info_sheet.delete_one({'_id':item_info['_id']})
